package com.hansen.library.utils;

import android.text.Editable;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2018/10/17 0017.
 */

public class StringUtils {
    private final static Pattern phone = Pattern
            .compile("^((13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$");
    private final static Pattern http = Pattern
            .compile("^(http|https)://.*");
    private final static Pattern emailer = Pattern
            .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    private final static Pattern colorRegex = Pattern.compile("^#[0-9a-fA-F]{6}$");

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }

    /**
     * 判断是不是一个合法的手机号码
     */
    public static boolean isPhone(String phoneNum) {
        if (phoneNum == null || phoneNum.trim().length() == 0)
            return false;
        return phone.matcher(phoneNum).matches();
    }

    /**
     * 判断是不是一个合法的电子邮件地址
     */
    public static boolean isEmail(String email) {
        if (email == null || email.trim().length() == 0)
            return false;
        if (email.contains("邮箱")) {
            return true;
        }
        return emailer.matcher(email).matches();
    }

    /**
     * 判断是不是一个合法的颜色
     */
    public static boolean isColor(String color) {
        if (isEmpty(color))
            return false;
        return colorRegex.matcher(color).matches();
    }

    /**
     * 判断是不是一个合法的电子邮件地址
     */
    public static boolean isHttp(String url) {
        if (url == null)
            return false;
        return http.matcher(url).matches();
    }

    /**
     * 判断是不是一个合法js交互
     */
    public static boolean isJsScheme(String url) {
        if (url == null)
            return false;
        return Pattern.compile("^(js://puguaweb).*").matcher(url).matches();
    }

    //检测是否是数字
    public static boolean isNumber(String number) {
        return !isEmpty(number) ? number.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$") : false;
    }

    public static boolean isIntNumber(String number) {
        return !isEmpty(number) ? number.matches("^-?\\d+$") : false;
    }

    //正整数
    public static boolean isPositiveIntNumber(String number) {
        return !isEmpty(number) ? number.matches("^[1-9]\\d*$") : false;
    }

    //大于0的两位小数的金额
    public static boolean isGratherZeroMoney(String money) {
        return !isEmpty(money) ? money.matches("(^[1-9](\\d+)?(\\.\\d{1,2})?$)|(^\\d\\.\\d{1,2}$)") : false;
    }

    public static boolean isEquals(String str, String targetStr) {
        if (str == null) {
            return false;
        }

        return str.equals(targetStr);
    }


    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }

    public static String getEditTextString(Editable editable) {

        return editable == null ? null : editable.toString().trim();
    }

    public static int getEditTextLength(Editable editable) {

        return editable == null ? 0 : editable.toString().trim().length();
    }

    public static int getTextLength(CharSequence charSequence) {

        return charSequence == null ? 0 : charSequence.toString().trim().length();
    }

    public static String getTextViewString(CharSequence sequence) {

        return sequence == null ? "" : sequence.toString().trim();
    }

    public static String getTextViewText(CharSequence sequence) {

        return sequence == null ? null : sequence.toString().trim();
    }

    //获取float型
    public static float getFloat(String number) {
        return isNumber(number) ? Float.parseFloat(number) : 0;
    }

    //获取int型
    public static int getInt(String number) {
        return isNumber(number) ? Integer.parseInt(number) : 0;
    }

    //大小判断
    public static boolean judgeGreaterNumber(String number, int targetNumber) {
        return isIntNumber(number) ? (Integer.parseInt(number) > targetNumber) : false;
    }

    //大小判断
    public static boolean judgeGreaterEqualNumber(String number, String targetNumber) {
        if (!isNumber(number) || !isNumber(targetNumber)) {
            return false;
        }

        return (new BigDecimal(number).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() >= new BigDecimal(targetNumber).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
    }

    //大小判断
    public static boolean judgeGreaterNumber(String number, float targetNumber) {
        if (!isNumber(number)) {
            return false;
        }

        BigDecimal bd = new BigDecimal(number);

        return bd.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() > targetNumber;
    }

    //大小判断
    public static boolean judgeGreaterNumber(String number, String targetNumber) {
        if (!isNumber(number) || !isNumber(targetNumber)) {
            return false;
        }

        return (new BigDecimal(number).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue() >= new BigDecimal(targetNumber).setScale(2, BigDecimal.ROUND_HALF_UP).floatValue());
    }

    //取绝对值
    public static int getMathAbs(String number) {
        return isIntNumber(number) ? Math.abs(getInt(number)) : 0;
    }

    public static String getNoNullString(String str) {

        return str == null ? "" : str;
    }

    public static String getNullEmptyConvertNone(String str) {

        return isEmpty(str) ? "无" : str;
    }

    public static String getNullEmptyConvert__(String str) {

        return isEmpty(str) ? "--" : str;
    }

    public static String getConvertMoney(String str) {

        return !isNumber(str) ? "0.00" : str;
    }

    public static String getPercentText(String str) {

        return isEmpty(str) ? "0%" : str + "%";
    }

    public static String getNullEmptyConvertZero(String str) {

        return isEmpty(str) ? "0" : str;
    }

    public static String getNullEmptyConvertZeroPrice(String str) {

        return isEmpty(str) ? "0.00" : str;
    }

    public static int getNullEmptyConvertIntZero(String str) {

        return isNumber(str) ? 0 : getInt(str);
    }

    public static int getNullEmptyConvertIntOne(String str) {

        return !isPositiveIntNumber(str) ? 1 : getInt(str);
    }

    public static String getGoodsPrice(String appPrice, String actPrice) {
        return judgeGreaterNumber(actPrice, 0f) ? actPrice : getNullEmptyConvert__(appPrice);
    }

    //生成已ASCII码升序排序
    public static String createKeySign(SortedMap<String, String> parameters) {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();

            sb.append((String) entry.getKey());
        }

        String sign = sb.toString();

        return sign;
    }

    //生成已ASCII码升序排序
    public static String createKeyValueSign(SortedMap<String, String> parameters) {
        StringBuffer sb = new StringBuffer();
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();

            sb.append((String) entry.getKey());
            sb.append((String) entry.getValue());
        }

        String sign = sb.toString();

        return sign;
    }

    //单个key-value组合Sign
    public static String createSigleValueSign(String key, String value) {
        StringBuffer sb = new StringBuffer();

        sb.append(key);
        sb.append(value);

        return sb.toString();
    }

    /**
     * 获取32位uuid
     *
     * @return
     */
    public static String get32UUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 生成唯一号
     *
     * @return
     */
    public static String get36UUID() {
        UUID uuid = UUID.randomUUID();
        String uniqueId = uuid.toString();
        return uniqueId;
    }
}
